<script setup>
import { ref,onMounted, watch } from 'vue';
import axios from 'axios';
import { ElMessage } from 'element-plus';
import IndexHeader from "@/components/IndexHeader.vue";
import VideoCard from "@/views/index/components/VideoCard.vue";
import FavoriteCard from "@/views/index/components/FavoriteCard.vue";
import {useRoute, useRouter} from "vue-router";
import User from "@/utils/User/User.js";
import VideoApi from "@/apis/VideoApi.js";
import {favoriteApi} from "@/apis/favoriteApi.js";
import UserViewUploads from "@/views/UserView/Components/UserViewUploads.vue";
import UserViewFavorites from "@/views/UserView/Components/UserViewFavorites.vue";
import UserViewFollow from "@/views/UserView/Components/UserViewFollow.vue";
import UserViewFan from "@/views/UserView/Components/UserViewFan.vue";
import UserAvatar from "@/components/UserAvatar/UserAvatar.vue";

const route = useRoute();
const router = useRouter();

const props = defineProps({
  userid: {
    type: String,
    required: true,
  }
})

const params = route.params;

const videos = ref([]);

const favorites = ref([]);

const userInfo = ref(null)

// 控制当前显示的内容
const currentView = ref('1'); // 默认显示视频

const fetchUserInfo = async () => {
  const res = await User.getUserInfo(params.userid)
  console.log('用户信息',res);
  userInfo.value = res;
}

const fetchUserVideos = async () => {
  const res = await VideoApi.fetchUserVideo();
  console.log('用户视频',res);
  videos.value = res.data.videos
}

const fetchFavorites = async () => {
  const favoritesRes = await favoriteApi.getCategoryList(props.userid)
  const fetchFavoritesPromises = favoritesRes.map(async favorite => {
    const res = await fetchFavoriteVideos(favorite)
    return {
      ...res.data,
      name: favorite,
    }
  })
  const result = await Promise.all(fetchFavoritesPromises)
  console.log(" 首页收藏夹result",result)
  favorites.value = result;
}

const fetchFavoriteVideos = async (cname) => {
  const res = await favoriteApi.getFavoriteVideos(props.userid,cname)
  console.log('收藏夹视频',cname,res);
  return res;
}

// function selectFavorite(key, keyPath) {
//   fav_index.value = `${key}`
//   selectedFavorite.value = favorite.value[key]
//   console.log(selectedFavorite.value)
// }

const handleSelect = (key, keyPath) => {
  currentView.value = `${key}`
}
function selectFollow(){
  currentView.value = '4'
}

function selectFan(){
  currentView.value = '5'
}

watch(route, (newValue, oldValue) => {
  router.go(0);
})

onMounted(() => {
  fetchUserInfo();
  fetchUserVideos();
  fetchFavorites();
  if(route.query.view === 'favorites') {
    currentView.value = '3'
  }
})

</script>

<template>
  <div class="header">
    <IndexHeader></IndexHeader>
    <img class="headerImg" src="@/assets/indexBgImg.png" alt="">
    <div class="up_info">
      <UserAvatar class="avatar"></UserAvatar>
      <div class="detail">
        <div class="username">
          {{ userInfo ? userInfo.userid : 0 }}
        </div>
<!--        <div class="abstract">-->
<!--          {{ 111111111111111 }}-->
<!--        </div>-->
      </div>
    </div>
  </div>
  <div class="nav">
    <div class="nav_left">
      <el-menu
          :default-active="currentView"
          class="el-menu-demo"
          mode="horizontal"
          @select="handleSelect"
          :ellipsis="false"

      >
        <el-menu-item class="left-item" index="1">主页</el-menu-item>
        <el-menu-item class="left-item" index="2">投稿</el-menu-item>
        <el-menu-item class="left-item" index="3">收藏</el-menu-item>
      </el-menu>
    </div>
    <div class="nav_right">
      <div class="follow" @click="selectFollow" :class="{ active: currentView === '4' }">
        <span class="right-item">关注数</span>
        <span class="right-num">{{ userInfo ? userInfo.follows_num : 0 }}</span>
      </div>
      <div class="fan" @click="selectFan" :class="{ active: currentView === '5' }">
        <span class="right-item">粉丝数</span>
        <span class="right-num">{{ userInfo ? userInfo.fans_num : 0 }}</span>
      </div>

    </div>
  </div>
  <div class="body">
    <div v-if="currentView === '2'">
      <UserViewUploads></UserViewUploads>
    </div>
    <div v-if="currentView === '1'">
      <div class="title_video">
        视频
      </div>

      <div class="video-grid" v-if="videos && videos.length > 0">
        <div v-for="(video, index) in videos?videos.slice(0,4):null" :key="index" class="video-card-container">
          <VideoCard :video="video" />
        </div>
      </div>
      <div v-else class="empty-message">
        这里什么都没有~
      </div>
      <div class="title_favorite">
        收藏夹
      </div>
      <div class="favorite-grid" v-if="favorites && favorites.length > 0">
        <div v-for="(favorite, index) in favorites" :key="index" class="favorite-card-container">
          <FavoriteCard :favorite="favorite" />
        </div>
      </div>
      <div v-else class="empty-message">
        这里什么都没有~
      </div>

    </div>
    <div v-if="currentView === '3'">
      <UserViewFavorites></UserViewFavorites>
    </div>
    <div v-if="currentView === '4'">
      <UserViewFollow></UserViewFollow>
    </div>
    <div v-if="currentView === '5'">
      <UserViewFan></UserViewFan>
    </div>
  </div>


</template>

<style scoped lang="less">
.header{
  width: 100%;
  height: 200px;
  display: flex;
  position: relative;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;

  .headerImg{
    height: 100%;
    width: 100%;
    position: absolute;
    left: 0;
    top: 0;
    object-fit: cover;
    object-position: center 50%;
    filter: brightness(95%);
    z-index: -10;
  }

  .up_info{
    width: 100%;
    position: relative;
    top: 60px;
    z-index: 0;
    flex: 1;
    display: flex;
    align-items: center;
    margin: 0 auto;
    height: fit-content;
    align-self: flex-end;
    padding-bottom: 22px;
    padding-left: 10%;

    .avatar{
      height: 64px;
      width: 64px;
      margin-left: 10%;
      border: 2px solid rgba(255, 255, 255, .4);
      border-radius: 50%;
    }

    .detail{
      flex: 1;
      margin-left: 30px;

      .username{
        font-size: 24px;
        font-weight: 700;
        color: white;
        margin-right: 8px;
        text-shadow: 0px 1px 2px rgba(0, 0, 0, .4);
      }

      .abstract{
        font-size: 17px;
        //font-weight: 700;
        color: white;
        margin-right: 8px;
        text-shadow: 0px 1px 2px rgba(0, 0, 0, .4);
      }

    }

  }
}

.nav{
  position: sticky;
  top: 0;
  height: 64px;
  width: 100%;
  background-color: white;
  box-shadow: 0 0 0 1px #e7e7e7;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 auto;
  padding: 0;
  z-index: 10;
  user-select: none;

  .nav_left{
    margin-left: 10%;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 64px;

    .el-menu-demo{
      height: 64px;
      border-bottom: none !important;

      .left-item{
        margin-right: 30px;
        font-size: 18px;
        font-weight: 700;

      }
    }
  }

  .nav_right{
    margin-right: 10%;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 64px;

    .follow, .fan{
      display: flex;
      flex-direction: column;
      align-items: center;
      margin: 0 20px;
      cursor: pointer;

      &.active {
        color: #409EFF; // 选中时的颜色
        .right-item, .right-num {
          color: #409EFF; // 选中时的颜色
        }
      }

      .right-item{
        font-size: 13px;
        line-height: 18px;
      }

      .right-num{
        font-size: 14px;
        line-height: 20px;
        margin-top: 2px;
        font-weight: 500;
      }
    }
  }
}

.body{
  margin-left: 10%;
  margin-right: calc(10% - 30px);
  margin-top: 20px;
  user-select: none;

  .title_video{
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin-bottom: 16px;
    height: 34px;
    font-size: 24px;
    font-weight: 600;
    color: black;
  }

  .video-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 30px;
    margin-bottom: 20px;
    box-shadow: 0 1px 0 0 #e7e7e7;;

    .video-card-container {
      width: calc(20% - 30px); // 5个卡片一行，减去间距
    }
  }

  .title_favorite{
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin-bottom: 16px;
    height: 34px;
    font-size: 24px;
    font-weight: 600;
    color: black;
  }

  .favorite-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 30px;
    margin-bottom: 20px;
    box-shadow: 0 1px 0 0 #e7e7e7;;

    .favorite-card-container {
      width: calc(20% - 30px); // 5个卡片一行，减去间距
    }
  }

  .empty-message {
     display: flex;
     justify-content: center;
     align-items: center;
     height: 100px;
     font-size: 16px;
     color: #999;
     margin: 20px 0;
   }

  .main-container {
    display: flex;
    //height: 100vh;

    .sidebar {
      width: 20%;
      max-width: 300px;
      background-color: white;
      padding: 16px;
      overflow-y: auto;

      .sidebar-favorites {

        .favorite-item {
          margin-bottom: 0;
          border-radius: 5px;
          overflow: hidden;
          //box-shadow: 1px 0 0 0 #409EFF;


          .favorite-name {
            //font-size: 18px !important;
            font-weight: 600;
          }
          .el-menu-item.is-active {
            color: #409EFF;
            background-color: #D5ECFE;
          }
        }
      }
    }

    .video-area {
      margin-left: 20px;
      flex-grow: 1;
      padding: 16px;
      overflow-y: auto;
      width: 65%;

      .video-grid {
        display: flex;
        flex-wrap: wrap;
        gap: 30px;
        margin-bottom: 20px;
        box-shadow: 0 1px 0 0 #e7e7e7;;

        .video-card-container {
          width: calc(20% - 30px); // 5个卡片一行，减去间距
        }
      }
    }
  }
}

</style>
